package ccapp;

import java.util.UUID;

import frmsystem.Domain;
import frmsystem.IAccount;
import frmsystem.IEntry;
import frmsystem.IParty;

public class CardSystem extends Domain{
	
	private static CardSystem instance;
	
	private CardSystem(){
		creator = new CardCreator();
	}
	
	public static CardSystem getInstance() {
		if(instance == null){
			instance = new CardSystem();
		}
		return instance;
	}

	@Override
	public void registerPerson(String name, String street, String city,
			String state, String zip, String email, String birthDate, int accType, String accNo) {
		IParty party = getParty(name);
		if(party==null){
			party = creator.createPerson(name, street, city, state, zip, email, birthDate);
			if(accNo==null){
				accNo = UUID.randomUUID().toString();
			}
			IAccount account = creator.createAccount(accNo, accType);
			party.openAccount(account);
	        account.addObserver(party);
			this.parties.add(party);
			this.accounts.add(account);	
		}else{
			if(accNo==null){
				accNo = UUID.randomUUID().toString();
			}
			IAccount account = creator.createAccount(accNo, accType);
			party.openAccount(account);
	        account.addObserver(party);
			this.accounts.add(account);	
		}
	}

	@Override
	public String[][] getAccountData() {
		String[][] strings = new String[this.accounts.size()][4];
		int i=0;
		for(IParty party : parties){
			for(IAccount acc : party.getAccounts()){
				strings[i][0] = party.getName();
				strings[i][1] = acc.getAccountNo();
				strings[i][2] = acc.getName();
				strings[i][3] = Double.toString(acc.getBalance());
				i++;
			}
		}
		
		return strings;
	}

	@Override
	public String[] getTitles() {
		return new String[]{"Name", "Account No", "Type", "Balance"};
	}
	
	
	
}
